//
//  GoodsDetailContentView.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/26.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "GoodsDetailContentView.h"
#import "GoodDetailPicsView.h"
#import <WebKit/WebKit.h>

@interface GoodsDetailContentView ()<UIScrollViewDelegate, WKUIDelegate, WKNavigationDelegate>

@property (strong, nonatomic) UIView *titleView;
@property (strong, nonatomic) UIScrollView *contentScrollView;

@end

@implementation GoodsDetailContentView

- (instancetype)init {
    
    if (self = [super init]) {
        
        self.backgroundColor = WhiteColor;
        
        [self addSubview:self.titleView];
        [self addSubview:self.contentScrollView];
    
        [self createUI];
        
        [self createTitleView];
        [self createContentScrollView];
        
    }
    
    return self;
    
}

- (void)setContentStr:(NSString *)contentStr {
    
    _contentStr = contentStr;
    
//    for (int i = 0; i < self.contentScrollView.subviews.count; i ++) {
    
    WKWebView *view = self.contentScrollView.subviews[0];
        
    NSString *str = [NSString stringWithFormat:@"%@",contentStr];
    str = [str stringByReplacingOccurrencesOfString:@"\\\"" withString:@"\""];
    str = [str stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"];
    str = [str stringByReplacingOccurrencesOfString:@"\\n" withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@"\\t" withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@"\"" withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@"\\r" withString:@""];
    
    [view loadHTMLString:str baseURL:nil];
        
//    }
    
}

#pragma mark -标题点击
- (void)lableClick:(UIGestureRecognizer *)recognizer {
    
    UILabel *label = (UILabel *)recognizer.view;
    
    [self.contentScrollView setContentOffset:CGPointMake(WIDTH * label.tag, 0) animated:YES];
    
    for (int i = 0; i < self.titleView.subviews.count; i ++) {
        
        UILabel *temp = self.titleView.subviews[i];
        
        temp.textColor = Color333333;
        temp.backgroundColor = WhiteColor;
    
    }
    
    label.textColor = WhiteColor;
    label.backgroundColor = Color333333;
 
}


#pragma mark -scrollviewDelegate
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    
    NSInteger index = scrollView.contentOffset.x / WIDTH;
    
    GoodDetailPicsView *contentView = self.contentScrollView.subviews[index];
    UILabel *label = self.titleView.subviews[index];
    
    for (int i = 0; i < self.titleView.subviews.count;  i ++) {
        
        UILabel *temp = self.titleView.subviews[i];

        temp.textColor = Color333333;
        temp.backgroundColor = WhiteColor;
    
    }

    label.textColor = WhiteColor;
    label.backgroundColor = Color333333;

    // 添加
    CGFloat vcW = scrollView.frame.size.width;
    CGFloat vcH = scrollView.frame.size.height;
    CGFloat vcY = 0;
    CGFloat vcX = index * vcW;
    contentView.frame = CGRectMake(vcX, vcY, vcW, vcH);
    
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
    
    WKNavigationResponsePolicy actionPolicy = WKNavigationResponsePolicyAllow;
    //这句是必须加上的，不然会异常
    decisionHandler(actionPolicy);
    
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    
    WKNavigationActionPolicy actionPolicy = WKNavigationActionPolicyAllow;
    
    decisionHandler(actionPolicy);
    
}

/// 2 页面开始加载
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    
    
}

/// 4 开始获取到网页内容时返回
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
    
    
}

/// 5 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    
    [webView evaluateJavaScript:@"document.body.scrollHeight" completionHandler:^(id _Nullable finishHeight, NSError * _Nullable error) {
        
        CGFloat newHeight = [finishHeight floatValue];
        NSLog(@"%.2f", newHeight);
        
//        self.contentScrollView.height = newHeight;
//        self.contentScrollView.frame = CGRectMake(0, jisuan(90), WIDTH, newHeight);
        
//        webView.height = newHeight;
        
        if (self.detailHeightBlock) {
            
            self.detailHeightBlock(newHeight);
        }
        
    }];
    
}


- (void)createUI {

    [self layoutIfNeeded];
    
    NSLog(@"%.2f", self.height);
    
    self.titleView.frame = CGRectMake(jisuan(20), jisuan(10), WIDTH - 2 * jisuan(20), jisuan(80));
    
    self.contentScrollView.frame = CGRectMake(0, jisuan(90), WIDTH, jisuan(934) - jisuan(90));

}

- (void)createTitleView {
    
    NSArray *array = @[@"图文详情", @"规格参数"];
    CGFloat width = (WIDTH - jisuan(20) * 2) / 2.0;
    
    for (int i = 0; i < 2; i ++) {
        
        UILabel *label = [[UILabel alloc] init];
        label.userInteractionEnabled = YES;
        label.font = Font(30);
        label.textAlignment = NSTextAlignmentCenter;
        label.text = array[i];
        label.layer.borderWidth = jisuan(2);
        label.layer.borderColor = Color333333.CGColor;
        
        if (i == 0) {
            
            label.backgroundColor = Color333333;
            label.textColor = WhiteColor;
            
        }else {
            
            label.backgroundColor = WhiteColor;
            label.textColor = Color333333;
        
        }
        
        label.frame = CGRectMake( width * i, 0, width, jisuan(80));
        [self.titleView addSubview:label];
        
        UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lableClick:)];
        [label addGestureRecognizer:recognizer];
    }
    
}


- (void)createContentScrollView {
    
    for (int i = 0; i < self.titleView.subviews.count; i++) {
        
//        GoodDetailPicsView *view = [[GoodDetailPicsView alloc] init];
//        view.htmlURL = self.contentStr;
//        [self.contentScrollView addSubview:view];
        
        WKWebView *webview = [self createWebView];

//        NSString *str = [NSString stringWithFormat:@"%@",self.contentStr];
//        str = [str stringByReplacingOccurrencesOfString:@"\\\"" withString:@"\""];
//        str = [str stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"];
//        str = [str stringByReplacingOccurrencesOfString:@"\\n" withString:@""];
//        str = [str stringByReplacingOccurrencesOfString:@"\\t" withString:@""];
//        str = [str stringByReplacingOccurrencesOfString:@"\"" withString:@""];
//        str = [str stringByReplacingOccurrencesOfString:@"\\r" withString:@""];
//
//        [webview loadHTMLString:str baseURL:nil];
        [self.contentScrollView addSubview:webview];
    
    }
    
    WKWebView *tempView = self.contentScrollView.subviews[0];
    tempView.frame = CGRectMake(0, 0, WIDTH, self.contentScrollView.height);
    
    self.contentScrollView.contentSize = CGSizeMake(WIDTH * self.contentScrollView.subviews.count, 0);
}

- (WKWebView *)createWebView {
    
    WKWebView *webView = [[WKWebView alloc] init];
    webView.UIDelegate = self;
    webView.navigationDelegate = self;
    
    webView.backgroundColor = Colorf2f2f2;
    webView.scrollView.scrollEnabled = YES;
    webView.scrollView.showsVerticalScrollIndicator = NO;
    webView.scrollView.showsHorizontalScrollIndicator = NO;
    
    if (@available(iOS 11.0 , *)) {
        
        webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        
    }
    
    
    return webView;
}

#pragma mark -lazy
- (UIView *)titleView {
    
    if (!_titleView) {
        
        _titleView = [[UIView alloc] init];
        _titleView.backgroundColor = WhiteColor;
//        _titleView.layer.borderColor = Color333333.CGColor;
//        _titleView.layer.borderWidth = jisuan(2);
        
    }
    
    return _titleView;
}

- (UIScrollView *)contentScrollView {
    
    if (!_contentScrollView) {
        
        _contentScrollView = [[UIScrollView alloc] init];
        _contentScrollView.backgroundColor = WhiteColor;
        
        _contentScrollView.showsVerticalScrollIndicator = NO;
        _contentScrollView.showsHorizontalScrollIndicator = NO;
        
        _contentScrollView.delegate = self;
        _contentScrollView.scrollEnabled = NO;
        
    }
    
    return _contentScrollView;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
